<?
require_once('./application/classes/item/album.class.php');

class albumModel extends db implements db_interface{

private static $instance;
public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
	
		public function fill(item_album $iAlbum){
		
		$returnArray = array();
		
		if($iAlbum -> album_type == 0){
		
		$query = $this -> db -> prepare('SELECT i.item_id,g.geb_naam,i.item_title,f.foto_path,f.foto_album_id,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join foto f on (i.item_id = f.foto_id) WHERE f.foto_album_id = ? AND i.item_deleted = 1');
		
		$bindValue = $iAlbum -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$path,$album_id,$date); 
		
		while ($query -> fetch())
		{
			$fotos = new item_foto($this -> db);
			$fotos -> foto_id = $id;
			$fotos -> foto_album_id = $album_id;
			$fotos -> title = $title;
			$fotos -> geb_naam = $geb_naam;
			$fotos -> foto_path = $path;
			
			$fotos -> datum = $date;
			
			$returnArray[] = $fotos;
		}
		
		$iAlbum -> fotos = $returnArray;
		
		
		}else{
	
			
		$query = $this -> db -> prepare('SELECT i.item_id,b.bew_titel,i.item_title,v.video_youtube_id,(SELECT item_title FROM item WHERE item_id = v.video_album_id) as album_naam, v.video_album_id ,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join video v on (i.item_id = v.video_id) join bewonersgroep b on (g.geb_id = b.bew_id) WHERE v.video_album_id = ? AND i.item_deleted = 1 AND TIMEDIFF(now(),i.item_datum) > "00:30:00"');
		
		$bindValue = $iAlbum -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$ytid,$album_naam,$album_id,$date); 
		
		while ($query -> fetch())
		{
			$video = new item_video($this -> db);
			$video -> id = $id;
			$video -> youtube_id = $ytid;
			$video -> geb_naam = $geb_naam;
			$video -> title = $title;
			$video -> album_id = $album_id;
			$video -> album_naam = $album_naam;
			$video -> datum = $date;
			
			$video -> getThumb();
			
			
			$returnArray[] = $video;
		}
		
		$iAlbum -> videos = $returnArray;
		
			
		
		
		}
	
	}
	
	
	
	public function getAllAlbums($iUser){
		$album = array();
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,i.item_title,a.album_type,DATE_FORMAT(i.item_datum, "%H:%i-%d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join album a on (i.item_id = a.album_id) join bewonersgroep bg on (g.geb_id = bg.bew_id) WHERE g.geb_id = ? AND i.item_deleted = 1;');
		
		$bindingVars = array($iUser -> id);
		
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
		
		$query -> bind_result($id,$bew_titel,$title,$type,$datum); 
		
		while ($query -> fetch())
		{
			$albums = new item_album($this -> db);
			$albums -> id = $id;
			$albums -> title = $title;
			$albums -> geb_naam = $bew_titel;
			$albums -> geb_id = $iUser -> id;
			$albums -> datum = $datum;
			
			$album[] = $albums;
		}
		
return $album;
	
	
	}
	
	
	public function getAlbums($iUser,$iType){
		$album = array();
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,i.item_title,a.album_type,DATE_FORMAT(i.item_datum, "%H:%i-%d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join album a on (i.item_id = a.album_id) join bewonersgroep bg on (g.geb_id = bg.bew_id) WHERE g.geb_id = ? AND a.album_type = ? AND i.item_deleted = 1;');
		
		$bindingVars = array($iUser -> id,$iType);
		
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
		
		$query -> bind_result($id,$bew_titel,$title,$type,$datum); 
		
		while ($query -> fetch())
		{
			$albums = new item_album($this -> db);
			$albums -> id = $id;
			$albums -> title = $title;
			$albums -> geb_naam = $bew_titel;
			$albums -> geb_id = $iUser -> id;
			$albums -> datum = $datum;
			
			$album[] = $albums;
		}
		
return $album;
	
	
	}
	
	public function addAlbums(item_album $album){
		$query = $this -> db -> prepare("CALL albumAdd(?,?,?,?)");	
		$bindingvars=array($album -> title , $album -> geb_id , 'album' , $album -> album_type);
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
	}
	public function delAlbum(item_album $album)
	{
		$query = $this -> db -> prepare('call albumDel(?);');
		$bindValue = $album->id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
	}
	public function updAlbum(item_album $album)
	{
		$query = $this -> db -> prepare('call albumUpd(?,?,?);');
		$bindingVars = array($album -> title , $album -> id , $album -> album_type);
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
	}
	public function fillAlbumId(item_album $album)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,bg.bew_id,i.item_title,a.album_type,DATE_FORMAT(i.item_datum, "%H:%i-%d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join album a on (i.item_id = a.album_id) join bewonersgroep bg on (g.geb_id = bg.bew_id) WHERE i.item_id = ? and i.item_deleted = 1;');
		
		$bindValue = $album -> id;
		
		$query -> bind_param('s',$bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$bew_titel,$geb_id,$title,$type,$datum); 
		
		$query -> fetch();
		
			$album -> title = $title;
			$album -> geb_id = $geb_id;
			$album -> geb_naam = $bew_titel;
			$album -> geb_id = $album -> geb_id;
			$album -> datum = $datum;
			$album -> album_type = $type;
			
			
	}

}



?>